

$(document).ready(function() {
        //var controls = GetControls();
        //var text_window =  CreateTextWindow();
        var book_tree = CreateList('testbog/ncc.html','book4','31402');
        
        var settings = GetSettings('31406');
        $("#nav #nav-content").html(book_tree); 
        $("#book-play #book-text-content").html(text_window);
        //$("#NccRootElement").attr("data-role", "listview");
        
        InsertSettings(settings);    

        PrepareGUIEvents();


    $("#search-form").submit(function(){
    	$.mobile.pageLoading();
    	$('#structure').empty();
        var formData = $("#search-form").serialize();
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "/Lyt/search.asmx/SearchFreetext",
            cache: false,
			data: '{term:"' + $('#search-term').val() + '"}',
            success: onSuccess,
            error: onError,
            complete: ref
        });
        return false;
    });
});

//SEARCH SUCCESS
function onSuccess(data, status)
{
	var books = data.d;
	var s = "";
	$.each(books, function(index, item) {
		s += '<li><a href="#book-details"><h3>' + item.title + '</h3><p>' + item.author + '</p></a><a href="#book-play">Afspil</a></li>';
	})
	$('#structure').html(s);
}

//SEARCH ERROR
function onError(msg, data)
{
    $("#structure").text("Error thrown: " + msg.status);

}       
//SEARCH COMPLETE
function ref()
{
	$('#structure').listview('refresh');
    $.mobile.pageLoading(true);
}       



function InsertControls(tag,controls)
{
    var tempList;
    tempList = controls.getElementsByTagName("a");
    container = document.createElement("ul");
    
    
    
    try
    {
    
        for(var j=0; j < tempList.length; j++)
        {
        
//            alert(tempList[j].innerHTML.toLowerCase());
          if(tempList[j].innerHTML.toLowerCase().indexOf("afspil") != -1 || tempList[j].innerHTML.toLowerCase().indexOf("pause") != -1)
          {
            var temp = document.createElement("li");
            temp.appendChild(document.importNode(tempList[j],true));
            container.appendChild(document.importNode(temp, true));
           }

        } 
        
        tag.innerHTML = container.innerHTML;
    
    }
    catch(e)
    {
        alert(e.message);
    
    }

}

function InsertSettings(set)
{
    $("#settings [type='checkbox']").attr("checked",true);
    //alert(set);
}

function SetSettings(user)
{
   
   var set= 
   { 
        textSize: "2", 
        markingColorBack: "yellow", 
        markingColorText: "black",
        textType: "Ariel",
   };    

   
   try
   {
        localStorage.setItem(user + "/mobileSettings", JSON.stringify(set));
   }
   catch(e)
   {
        alert(e.message);
   
   }


}


function GetSettings(user)
{
    var settings;
    
    if (!supports_local_storage())
    {
        alert(aLang.Translate("MESSAGE_NO_LOCAL_STORAGE"));      
    }
    else
    {
        try
        {
             settings = JSON.parse(localStorage.getItem(user + "/mobileSettings"));
             if(settings == null)
             {
                var set= 
                { 
                    textSize: "1", 
                    markingColorBack: "yellow", 
                    markingColorText: "black",
                    textType: "Ariel",
                };    
                localStorage.setItem(user + "/mobileSettings", JSON.stringify(set));
                settings = set;
             }
             
        
        }
        catch(e)
        {
            alert(e.message);
        
        }
    
    }
    
    return settings;
}


function PrepareGUIEvents()
{
    try
    {
                $("#NccRootElement a").click
                (
                    function() 
                    {
                         GetTextAndSound(this);
                         window.location.hash = "#book-play";
                         
                    }
                );
                
                $("#bookshelf #bookshelf-content a").click
                (
                    function() 
                    {
                    
                         GetTextAndSound(AhrefList[0]);//Global array from Fileinterface....with book refs...
                         
                    }
                );
                

                $("#nav").live("swiperight", function()
                {
       
                     window.location.hash = "#book-play";
                    
                });
                
                $("#settings").live("swiperight", function()
                {
       
                     window.location.hash = "#book-play";
                    
                });
                
                
                $("#book-play").live("pageshow", function()
                {
                    setTimeout("$('#book-play').click();",2000);
                    
                });
                
                
                $("#settings").live("pagehide", function()
                {
                   SetSettings('31402');
                   
                    
                });
                
                
                

    
    }
    catch(e)
    {
            alert(e.message);

    }



}
